/**
	Accounts controller handles all interactions with accounts.
 */
Ext.define('WEB.controller.Accounts', {
    extend: 'Ext.app.Controller',
	views: [
//		'account.Manage',
		'account.List',
//		'account.SimpleList',
//		'account.Edit',
		'account.Create'
	],
	stores: [
		'Accounts'
	],
	models: [
		'Account'
	],
	
	/*
		Setup event handlers.
	*/
    init: function() {
		this.control({
			'accountlist toolbar button[action=add]': {
				click: this.openNewAccount
			},
			'accountlist toolbar button[action=delete]': {
				click: this.deleteAccount
			},
			'accountlist toolbar button[action=update]': {
				click: this.editAccount
			},
			'accountlist': {
				selectionchange: this.setButtonsState
			},
			/*'accountmanage button[action=editaccount]': {
				click: this.openEditAccount
			},
			'accountmanage button[action=addaccount]': {
				click: this.openNewAccount
			},
			'accountmanage button[action=deleteaccount]': {
				click: this.deleteAccount
			},
			'accountmanage accountlist': {
				selectionchange: this.setButtonsState
			},
			'accountmanage': {
				show: this.refreshStore
			},
			'accountedit button[action=save]': {
				click: this.updateAccount
			},
			'accountedit button[action=newcurrency]': {
				click: function() {this.getController('Currencies').openNewCurrency();}
			},*/
			
			
			'accountcreate button[action=save]': {
				click: this.createAccount
			},
			'accountcreate button[action=update]': {
				click: this.updateAccount
			}
			
			/*,
			'accountcreate button[action=newcurrency]': {
				click: function() {this.getController('Currencies').openNewCurrency();}
			}*/
		});//this.control
    },//init
	
	/*
		Open edit account dialogue and load account information.
		This is an event handler for accounts list double click action.
		Parameters:
			grid   - grid panel that the user double clicked
			record - account record that the user double clicked on
	*/
	/*editAccount: function(grid, record) {
		var view = Ext.widget('accountcreate');
		view.down('form').loadRecord(record);
	},*///editAccount
	
	/*
		The same as editAccount but for "Edit" button on the toolbar.
		Since we don't have reference to the grid or selected record
		we have to find out which record is selected and then call 
		editAccount method. If no records are selected an error message
		will be shown.
	*/
	/*openEditAccount: function (button) {
		var win  = button.up('window'),
			grid = win.down('accountlist'),
			sel  = grid.getSelectionModel(),
			recs = sel.selected;
			if (recs.items.length <= 0) {
				Ext.Msg.alert('Error', 'Please select one of the accounts first.');
			} else {
				this.editAccount(grid, recs.items[0]);
			}
	},*///openEditAccount
	
	/*
		Open new account dialogue. Nothing exciting here - just create
		the view.
	*/
	openNewAccount: function(button) {
		var view = Ext.widget('accountcreate');
		view.down('button[action=update]').setVisible(false);
	},//openNewAccount

	/*
		An event handler for "Save" button in the edit account dialogue.
		Collects the values from the form, updates the record and calls
		sync on the store which triggers communication with the server.
		Any error during the communication is handled by the model's proxy.
	*/
	updateAccount: function(button) {
		var win    = button.up('window'),
			form   = win.down('form'),
			record = form.getRecord(),
			values = form.getValues();

		record.set(values);
		this.getAccountsStore().sync();
		//record.commit(); //Should not be necessary, but there is a known bug
		                 //in ExtJs. Will be removed once the bug is fixed.
		win.close();
	},//updateAccount
	
	editAccount: function(button) {
		var grid    = button.up('accountlist'),
			sel  = grid.getSelectionModel(),
			recs = sel.selected;
		if (recs.items.length <= 0) {
			Ext.Msg.alert('Error', 'Please select one of the accounts first.');
		} else {
			var record = recs.items[0];
			var view = Ext.widget('accountcreate');
			view.setTitle('Edit Account');
			view.down('button[action=save]').setVisible(false);
			view.down('button[action=update]').setVisible(true);
			view.down('form').loadRecord(record);
		}
	},
	
	
	/*
		An event handler for "Save" button in the create account dialogue.
		Collects the values from the form, create new record, adds it to
		the store and calls	sync on the store which triggers communication
		with the server. Any error during the communication is handled by 
		the model's proxy.
	*/
	createAccount: function(button) {
		var win    = button.up('window'),
			form   = win.down('form'),
			values = form.getValues(),
			store  = this.getAccountsStore();

		var record = this.getAccountModel().create(values);

		store.add(record);
		store.sync();
		win.close();
	},//createAccount
	
	/*
		An event handler for "Remove" button in the manage accounts dialogue.
		Asks for confirmation and if positive destroys the selected record.
		Any error during the communication is handled by the model's proxy.
	*/
	deleteAccount: function(button) {
		var grid    = button.up('accountlist'),
			sel  = grid.getSelectionModel(),
			recs = sel.selected;
		if (recs.items.length <= 0) {
			Ext.Msg.alert('Error', 'Please select one of the accounts first.');
		} else {
			var record = recs.items[0];
			Ext.Msg.show({
				title:'Please confirm',
				msg: 'Are you sure you want to delete this account?',
				buttons: Ext.Msg.YESNO,
				fn: function(buttonId) {
					if (buttonId == 'yes') {
						record.destroy();
					}
				},
				icon: 'atb-icon-question'
			});
		}
	},//deleteAccount

	/*
		Event handler for accounts list. Disables/enables "Edit" and "Remove"
		buttons according to selection.
	*/
	setButtonsState: function(view, sel, opts) {
		var win    = view.view.up('panel'),
			uptBtn = win.down('button[action=update]'),
			delBtn = win.down('button[action=delete]')
		if (sel.length > 0) {
			uptBtn.enable();
			delBtn.enable();
		} else {
			uptBtn.disable();
			delBtn.disable();
		}
	},//setButtonsState

	/*
		Refresh accounts store when manage accounts dialogue is shown to
		fetch any changes from the server before we make new ones.
	*/
	refreshStore: function() {
		this.getAccountsStore().load();
	}//refreshStore
});
